package middleware.mq;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * 生产者端
 * 同步信息
 *
 * 场景：
 * 重要的消息通知，短信通知
 *
 * Author: shen qi han
 * Date: 2022/4/3 下午10:13
 */
public class SyncProducerMain {

    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, MQBrokerException, RemotingException, InterruptedException {
        // init the producer
        DefaultMQProducer producer = new DefaultMQProducer("taskGroup");
        // set the nameServer addr
        producer.setNamesrvAddr("localhost:9876");
        // start instance
        producer.start();

        // item msg
        Message msg = new Message("TopicTask",
                "TagML",
                "jsonInfo".getBytes(RemotingHelper.DEFAULT_CHARSET));
        // send to broker
        SendResult result = producer.send(msg);
        System.out.println(result);

        // shut down
        producer.shutdown();
    }

}
